<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BUISpace
 *
 * @author jianfeng
 */
class BUISpace extends BUINode {

    public $layoutView = null;
    public $views = null;
    public $space = null;

    public function __construct($node = NULL) {
        parent::__construct($node);
        $this->space = $node;

        $basePath = $node->getConfigManager()->getConfigValue('basePath');

        $uiPath = $basePath . '.ui';

        if ($this->viewPath == NULL)
            $this->viewPath = $uiPath . ".themes." . Blyn::app()->getAppUI()->getTheme() . '.views';

        if ($this->formPath == NULL)
            $this->formPath = $uiPath . ".themes." . Blyn::app()->getAppUI()->getTheme() . '.forms';
    }

    public function onRender($data = NULL) {
        parent::onRender($data);
        if ($this->views == NULL)
            $this->views = array();
        $this->views['spaceTopBar'] = array('view' => 'space_topBar', 'data' => array('uiObj' => $this));

        //if (Blyn::app()->getActiveApp()->getId() == Blyn::app()->getCurrentUser()->getPersonalSpace()->getAppByName('BPersonalApp')->getId())
        //    $this->views['spaceTopBar'] = array('view' => 'personalSpaceTopBar', 'data' => array('uiObj' => $this));
        //add ui structure in page
        if (Blyn::app()->getActiveApp() != NULL) {
            $uiApp = Blyn::app()->getActiveApp()->getUIManager();
        } else {
            $uiApp = new BUIApp();
        }
        $this->addControl($uiApp);

        /*

          if (Blyn::app()->getActiveOperationFlow() != NULL) {
          $uiOpFlow = Blyn::app()->getActiveOperationFlow()->getUIManager();
          } else {
          $uiOpFlow = new BUIOperationFlow();
          }
          $uiApp->addControl($uiOpFlow);

          if (Blyn::app()->getActiveOperation() != NULL) {
          $uiOp = Blyn::app()->getActiveOperation()->getUIManager();
          } else {
          $uiOp = new BUIOperation();
          }
          $uiOpFlow->addControl($uiOp);

          if (Blyn::app()->getUIManager()->getCurrentScreenFlow() != NULL) {
          $uiScnFlow = Blyn::app()->getUIManager()->getCurrentScreenFlow();
          } else {
          $uiScnFlow = new BUIScreenFlow('');
          }
          $uiOp->addControl($uiScnFlow);

          if (Blyn::app()->getUIManager()->getCurrentScreen() != NULL) {
          $uiScreen = Blyn::app()->getUIManager()->getCurrentScreen();
          } else {
          $uiScreen = new BUIScreen();
          }
          $uiScnFlow->addControl($uiScreen);
         * 
         */
    }

    public function renderCreateSpace($config = null) {
        //$screenName = isset($_POST['bilyn_screen_name'])?$_POST['bilyn_screen_name']:'CreateSpaceMeta';
        //return $this->gotoScreen($screenName,$this,$config,$style);
        return $this->gotoScreenFlow('BScreenCreateSpace', $this, $config);
    }

    public function renderJoinSpace($config = null) {
        return $this->gotoScreen('BScreenJoinSpace', $this, $config);
    }

    public function getUrl($data = NULL) {
        $ajaxUrl = $this->getAjaxUrl($data);

        if ($ajaxUrl != NULL)
            return $ajaxUrl;

        return $this->buildUrl($this->space->getId(), NULL, NULL, $data);
    }

    public function getAjaxLink($name, $data = array(), $sender = NULL, $route = 'site/index', $upateControl = NULL) {
        if ($sender == NULL)
            $sender = $this;

        $currentSpace = $this->getNode();
        $activeSpace = Blyn::app()->getActiveSpace();

        if ($currentSpace->getId() != $activeSpace->getId()) {
            return Blyn::app()->getUIManager()->getAjaxLink($name, $data, $sender);
        } else {
            return parent::getAjaxLink($name, $data, $sender, $route, $upateControl);
        }
    }
    
    public function getAjaxContainerId($data = NULL) {
        return 'b_show_space_content';
    }
    
    public function getAngularControllerDefine($data = NULL) {
        return 'bngSpaceController';
    }

    /*
      public function getAjaxLink($name, $data = NULL) {
      return parent::createAjaxLink($name, $this->getUrl(), BUIBase::ajaxContainter_Space, $this->getLayoutView(), $data);
      }
     * 
     */
}

?>
